<?php

namespace App\Http\Controllers\Main;

use App\Model\NewsCategory;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Model\News;

class NewsController extends Controller
{
    //
    public function detail($id) {
        $data = News::find($id);
        $list = News::select('id','title')->where('recomm',1)->where('status',1)->orderBy('create_time','desc')->get();
        $time = substr($data->create_time,0,10);
        $t = explode('-',$data->make_time);
        return view('main.news.detail',compact('data','list','t'));
    }

    public function newsList() {
        $data = News::select('id','title','create_time','picture','make_time')->where('status',1)->orderBy('create_time','desc')->paginate(9);
        $list = News::select('id','title')->where('recomm',1)->where('status',1)->orderBy('create_time','desc')->get();
        foreach ($data as $d => $value) {
            $data[$d]['create'] = substr($value['create_time'],0,10);
        }
        $title = "全部新闻";
        return view('main.news.list',compact('data','title'));
    }

    public function cateList($cid) {
        $limit = null;
        $data = News::select('id','title','create_time','picture','make_time')->where('category_id',$cid)->where('status',1)->orderBy('create_time','desc')->paginate(15);
        $list = News::select('id','title')->where('recomm',1)->where('status',1)->orderBy('create_time','desc')->get();
        foreach ($data as $d => $value) {
            $data[$d]['create'] = substr($value['create_time'],0,10);
        }
        $title = NewsCategory::find($cid)['name'];
//        if($cid == 3) {
//            return view('main.news.list',compact('data','title'));
//        }
//        else {
            return view('main.news.state',compact('data','title'));
//        }
    }
}
